Imports BlazeApps.Library.Blog
Imports BlazeApps.Library.News

Partial Class Usercontrols_Blog_addblog
    Inherits UserControlBase

#Region " Enums, Memebers and Properties "

    Protected Property BlogGuid() As Guid
        Get
            Return GetGuidFromVS("BlogID")
        End Get
        Set(ByVal value As Guid)
            ViewState.Add("BlogID", value)
        End Set
    End Property

    Protected Property NewButtonClicked() As Boolean
        Get
            Return GetBooleanFromVS("NewButtonClicked")
        End Get
        Set(ByVal value As Boolean)
            ViewState.Add("NewButtonClicked", value)
        End Set
    End Property

#End Region

#Region " Page Events "

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.MessageLabel = Me.uxMessagesLabel
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        SetPageTitle("Create Blog")

        If IsUserAuthenticated And (EnableUserBlogs OrElse IsBlogModerator()) Then
            GetQueryStringValues()
            ConfigBaseEditorCtrls()

            If Me.Page.IsPostBack = False Then
				'BuildEditor(EditorPageType.SingleEditor)
				BuildFCKEditor(EditorPageType.SingleEditor)

                If Not (BlogGuid = Guid.Empty) Then
                    LoadBlog()
                End If
            End If

            LoadSettings()

            Me.uxAddToNewsLabel.Visible = ShowControls(SecurityMask.Editor, False)
            Me.uxAddToNewsCheckbox.Visible = ShowControls(SecurityMask.Editor, False)

            Me.uxMemberBlogPanel.Visible = ShowControls(SecurityMask.Member, True)

        Else
            Me.uxMemberBlogPanel.Visible = False
            Me.uxMessagesLabel.Text = Resources.Resource.AdminDisabledBlogs.ToString
        End If

    End Sub

#End Region

#Region " Save Data "

    Private Sub SaveBlog()
        Dim Item As BlogDetail = BlogDetail.NewBlogDetail()

        Item.Title = Me.uxBlogTitleTextBox.Text.ToString.Trim
		Item.Body = Me.uxBlogBodyTextbox.Value.ToString.Trim
        Item.Private = Me.uxPrivateBlogEntryCheclBox.Checked
        Item.Userid = CurrentUserID
        Item.CreatedateString = DateTime.Now.ToString

        Item.Save()

        If uxAddToNewsCheckbox.Checked Then
            Dim NewsItem As NewsDetail = NewsDetail.NewNewsDetail

            NewsItem.title = Me.uxBlogTitleTextBox.Text
			NewsItem.body = Me.uxBlogBodyTextbox.Value
			NewsItem.expiredateString = DateTime.Now.AddMonths(6)
			NewsItem.Sticky = False
			NewsItem.Userid = CurrentUserID
			NewsItem.Save()
		End If

		BlogGuid = Item.Blogid

		uxOptions.ItemGuid = Item.Blogid
		uxOptions.SetInitialOptions = True
		uxOptions.SaveOptions()
		uxOptions.LoadOptions()

		LoadSettings()

	End Sub

	Private Sub UpdateBlog()
		Dim Item As BlogDetail = BlogDetail.GetBlogDetail(BlogGuid)

		Item.Title = Me.uxBlogTitleTextBox.Text.ToString.Trim
		Item.Body = Me.uxBlogBodyTextbox.Value.ToString.Trim
		Item.Private = Me.uxPrivateBlogEntryCheclBox.Checked

		Item.Save()

	End Sub

#End Region

#Region " Load Data "

	Private Sub LoadSettings()
		If (BlogGuid = Guid.Empty) Then
			Me.uxOptionsTabPanels.Enabled = False
			Me.uxMyGalleryTabPanel.Enabled = False
			Me.uxAttachmentsTabPanel.Enabled = False
		Else
			Me.uxOptionsTabPanels.Enabled = True
			uxMyGalleryTabPanel.Enabled = (EnableUserGalleries OrElse IsAdmin())
			uxAttachmentsTabPanel.Enabled = (EnableUserGalleries OrElse IsAdmin())
		End If

		Me.uxMyGallery.IsLoadedAsOption = True

	End Sub

	Private Sub LoadBlog()

		Dim Item As BlogDetail = BlogDetail.GetBlogDetail(BlogGuid)

		Me.uxBlogTitleTextBox.Text = Item.Title
		Me.uxBlogBodyTextbox.Value = Item.Body
		Me.uxPrivateBlogEntryCheclBox.Checked = Item.Private

	End Sub

#End Region

#Region " Form Controls Events "

	Protected Sub uxNewBlogButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxNewBlogButton.Click
		'Response.Redirect(String.Format("{0}.aspx?pid=blg_addblog", AppPage))

		NewButtonClicked = True

		BlogGuid = Guid.Empty
		LoadSettings()

		Me.uxBlogTitleTextBox.Text = String.Empty
		Me.uxBlogBodyTextbox.Value = String.Empty
		Me.uxPrivateBlogEntryCheclBox.Checked = False

	End Sub

	Protected Sub uxSubmitBlogButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxSubmitBlogButton.Click
		If BlogGuid = Guid.Empty Then
			SaveBlog()
		Else
			UpdateBlog()
		End If
	End Sub

	Protected Sub uxDeleteBlogButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxDeleteBlogButton.Click
		BlogDetail.DeleteBlogDetail(BlogGuid)

		Response.Redirect(String.Format("{0}.aspx?pid=blg_bloglist", AppPage))
	End Sub

	Protected Sub uxResetBlogButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxResetBlogButton.Click
		If Not (BlogGuid = Guid.Empty) Then
			LoadBlog()
		Else
			Me.uxBlogTitleTextBox.Text = String.Empty
			Me.uxBlogBodyTextbox.Value = String.Empty
			Me.uxPrivateBlogEntryCheclBox.Checked = False
		End If

		LoadSettings()
	End Sub

#End Region

#Region " Helpers and Methods "

    Protected Sub GetQueryStringValues()
        If NewButtonClicked = False Then
            BlogGuid = GetGuidFromQueryString("id")
        End If
    End Sub

    Private Sub ConfigBaseEditorCtrls()
		'Me.FTBEditor = Me.uxBlogBodyTextbox
		Me.FCKEditor = uxBlogBodyTextbox
    End Sub

#End Region

End Class
